﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using GiaPhuc.Data;
using System.Data;
using GiaPhuc.Model;

namespace GiaPhuc
{
    /// <summary>
    /// Summary description for CMS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CMS : System.Web.Services.WebService
    {

        #region Topic Methods
        [WebMethod, ScriptMethod]
        public GridTable<EZPages> LoadTopics(int pageNumber)
        {
            int totalRecords = 0;
            GridTable<EZPages> gridTopics;

            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
            {
                using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                {
                    totalRecords = context.EZPages.Count();
                    var collection = context.EZPages.OrderBy(x => x.PagesID).Skip((pageNumber - 1) * define.PAGE_SIZE).Take(define.PAGE_SIZE).ToList().Select(x => new
                    {
                        PagesID = x.PagesID,
                        PagesTitle = x.PagesTitle,
                        ToCChapter = x.ToCChapter
                    });

                    IList<EZPages> topics = new List<EZPages>();
                    if (totalRecords > 0)
                    {
                        foreach (var item in collection)
                        {
                            topics.Add(new EZPages
                            {
                                PagesID = item.PagesID,
                                PagesTitle = item.PagesTitle,
                                ToCChapter = item.ToCChapter
                            });
                        }
                    }

                    gridTopics = new GridTable<EZPages>
                    {
                        PageNumber = pageNumber,
                        TotalRecords = totalRecords,
                        PageSize = define.PAGE_SIZE,
                        List = topics,
                    };
                }
            }

            return gridTopics;
        }

        [WebMethod, ScriptMethod]
        public bool DeleteTopic(int ID)
        {
            bool bflag = false;

            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
            {
                using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                {
                    var page = context.EZPages.SingleOrDefault(x => x.PagesID == ID);

                    if (null != page)
                    {
                        context.EZPages.DeleteOnSubmit(page);
                        context.SubmitChanges();

                        bflag = true;
                    }
                }
            }

            return bflag;
        }
        #endregion

        #region Product Methods
        [WebMethod, ScriptMethod]
        public GridTable<ProductModel> LoadProducts(int pageNumber)
        {
            int totalRecords = 0;
            GridTable<ProductModel> gridProducts;

            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
            {
                using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                {
                    totalRecords = context.Products.Count();
                    var collection = from x in context.Products.OrderBy(x => x.ProductsID).Skip((pageNumber - 1) * define.PAGE_SIZE).Take(define.PAGE_SIZE).ToList()
                                     join y in context.ProductsDescription
                                     on x.ProductsID equals y.ProductsID
                                     select new { x, y };

                    IList<ProductModel> products = new List<ProductModel>();
                    if (totalRecords > 0)
                    {
                        foreach (var item in collection)
                        {
                            products.Add(new ProductModel
                            {
                                ProductID = item.x.ProductsID,
                                ProductName = item.y.ProductsName,
                                Price = item.x.ProductsPrice,
                                ImagePath = item.x.ProductsImage
                            });
                        }
                    }

                    gridProducts = new GridTable<ProductModel>
                    {
                        PageNumber = pageNumber,
                        TotalRecords = totalRecords,
                        PageSize = define.PAGE_SIZE,
                        List = products,
                    };
                }
            }

            return gridProducts;
        }

        [WebMethod, ScriptMethod]
        public bool DeleteProduct(int ID)
        {
            bool bflag = false;

            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
            {
                using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                {
                    var product = context.Products.SingleOrDefault(x => x.ProductsID == ID);

                    if (null != product)
                    {
                        var descriptions = context.ProductsDescription.Where(x => x.ProductsID == ID);
                        if (null != descriptions)
                        {
                            context.ProductsDescription.DeleteAllOnSubmit(descriptions);
                        }

                        context.Products.DeleteOnSubmit(product);
                        context.SubmitChanges();

                        bflag = true;
                    }
                }
            }

            return bflag;
        }
        #endregion

    }

    public class GridTable<T>
    {
        public int PageNumber { get; set; }
        public int TotalRecords { get; set; }
        public int PageSize { get; set; }
        public IList<T> List { get; set; }
    }
}
